import React from 'react';
import {
  AsyncStorage,
} from 'react-native';

import Config from '../config';
import Constants from '../common/Constants';
import ResponseConstants from '../common/ResponseConstants';
import BaseModelNew from './BaseModelNew';

export default class QuanZiModel extends BaseModelNew {
  constructor() {
    super();

    this.createPost = this.createPost.bind(this);
    this.sendQuanziList = this.sendQuanziList.bind(this);
    this.sendRequestQuanziAllList = this.sendRequestQuanziAllList.bind(this);
    this.sendRequestQuanziContent = this.sendRequestQuanziContent.bind(this);
    this.sendRequestQuanziHoemTuijian = this.sendRequestQuanziHoemTuijian.bind(this);
    this.sendRequestQuanziPostList = this.sendRequestQuanziPostList.bind(this);
    this.sendQuanziPostComment = this.sendQuanziPostComment.bind(this);
    this.sendRequestQuanziPostContent = this.sendRequestQuanziPostContent.bind(this);
    this.sendRequestQuanziCommentList = this.sendRequestQuanziCommentList.bind(this);
    this.sendQuanziChatGroupList = this.sendQuanziChatGroupList.bind(this);
    this.requestQuanziRandomList = this.requestQuanziRandomList.bind(this);
    this.sendShoucangQuazi = this.sendShoucangQuazi.bind(this);
    this.sendRequestSearchList = this.sendRequestSearchList.bind(this);
    this.requestCreateQunliao = this.requestCreateQunliao.bind(this);
    this.requestQuanziHotList = this.requestQuanziHotList.bind(this);
    this.requestQuanziMoreRecommendList=this.requestQuanziMoreRecommendList.bind(this);
    this.requestQunliaoList = this.requestQunliaoList.bind(this);
    this.requestQuitQun = this.requestQuitQun.bind(this);
    this.requestEnterQun = this.requestEnterQun.bind(this);
    this.requestQunMemberList = this.requestQunMemberList.bind(this);
    this.requestQunData = this.requestQunData.bind(this);
    this.sendRequestSearchContent = this.sendRequestSearchContent.bind(this);
    this.requestPostLike = this.requestPostLike.bind(this);
    this.requestQuaziMembers = this.requestQuaziMembers.bind(this);
    this.requestQuaziFind = this.requestQuaziFind.bind(this);
    this.requestQuaziMine = this.requestQuaziMine.bind(this);
    this.requestPostShoucang = this.requestPostShoucang.bind(this);
    this.sendUpdateQunData = this.sendUpdateQunData.bind(this);
    this.sendKickMember = this.sendKickMember.bind(this);
    this.sendGetMembersUncheckedNum = this.sendGetMembersUncheckedNum.bind(this);
    this.sendGetMembersUncheckedList = this.sendGetMembersUncheckedList.bind(this);
    this.sendCheckMember = this.sendCheckMember.bind(this);
    this.sendCreateQuanzi = this.sendCreateQuanzi.bind(this);
    this.requestQuanziUnchecked =this.requestQuanziUnchecked.bind(this);
    this.requestQuanziUncheckedList= this.requestQuanziUncheckedList.bind(this);
    this.requestQuanziCommentsUncheckedList = this.requestQuanziCommentsUncheckedList.bind(this);
    this.requestQuanziFollowTo = this.requestQuanziFollowTo.bind(this);
    this.requestQuanziFollowMe = this.requestQuanziFollowMe.bind(this);
    this.requestQuanziFollowToPosts = this.requestQuanziFollowToPosts.bind(this);
    this.requestFollowUser = this.requestFollowUser.bind(this);
    this.requestQuanziReply2me = this.requestQuanziReply2me.bind(this);
    this.requestQuanziPostOrCommet = this.requestQuanziPostOrCommet.bind(this);
    this.requestQuanziOwned = this.requestQuanziOwned.bind(this);
    this.requestQuanziMyposts = this.requestQuanziMyposts.bind(this);
    this.requestQuanziMyspace = this.requestQuanziMyspace.bind(this);
    this.requestQuanziFollowPosts = this.requestQuanziFollowPosts.bind(this);
    this.requestReportPostOrCommet = this.requestReportPostOrCommet.bind(this);
    this.requestUpdateTop = this.requestUpdateTop.bind(this);
    this.requestHostList = this.requestHostList.bind(this);
    this.sendQuanziCommentShow = this.sendQuanziCommentShow.bind(this);
    this.sendQuanziApply = this.sendQuanziApply.bind(this);
    this.requestAdminListAction = this.requestAdminListAction.bind(this);
    this.requestMyCreatedAction = this.requestMyCreatedAction.bind(this);
    this.requestMyCreatedAction = this.requestMyCreatedAction.bind(this);
    this.requestMyManagedAction = this.requestMyManagedAction.bind(this);
    this.requestSearchBoardUser = this.requestSearchBoardUser.bind(this);
    this.sendUpdateQZSetting =  this.sendUpdateQZSetting.bind(this);
    this.sendQuanziIsAdminAction = this.sendQuanziIsAdminAction.bind(this);
    this.requestQZDetailAction = this.requestQZDetailAction.bind(this);
  }

  //发表圈子帖子 CreatePostPayload
  createPost(data, callback) {
    var sendData = {
      "content": data.content,
      "title": data.title,
       "image_urls": data.image_urls,
    }
    this.sendPost(Config.serverUrl + '/boards/' + data.board_id + '/posts', JSON.stringify(sendData), callback);
  }

  //GET /dqapp/boards/my_created
  sendRequestQuanziAllList(data, callback) {
    var url = Config.serverUrl + "/boards?type=2&page="+data.page+"&page_size=10&sort_id=2";
    this.sendGet(url, callback);
  }
  requestMyCreatedAction(data, callback) {
    var url = Config.serverUrl + "/boards/my_created?page="+data.page+"&page_size="+data.page_size;
    this.sendGet(url, callback);
  }
  requestMyManagedAction(data, callback) {
    var url = Config.serverUrl + "/boards/my_managed?page="+data.page+"&page_size="+data.page_size;
    this.sendGet(url, callback);
  }
  requestQZDetailAction(data, callback) {
    var url = Config.serverUrl + "/boards/"+data.board_id+"/detail";
    this.sendGet(url, callback);
  }
  sendRequestSearchList(data, callback) {
    var url = Config.serverUrl + "/boards/searched?favorited=0&&type=2&page="+data.page+"&page_size=10&sort_id=1&key="+data.key;
    this.sendGet(url, callback);
  }
  //热门圈子  GET /dqapp/boards/hotspots
  requestQuanziHotList(data, callback) { 
    var url = Config.serverUrl + "/boards/hotspots";
    this.sendGet(url, callback);
  }
  //GET /dqapp/boards/search_board_user
  requestSearchBoardUser(data, callback) {
    var url = Config.serverUrl + "/boards/search_board_user";
    if(data.board_id!==undefined) {
      url += "?board_id=" + data.board_id;
    }
    url += "&search_word="+data.search_word;
    url += "&page=1";
    url += "&page_size=200";
    this.sendGet(url, callback);
  }
  //获取未审核的帖子和评论数
  requestQuanziUnchecked(data, callback) { 
    var url = Config.serverUrl + "/boards/posts/unchecked/count";
    if(data.board_id!==undefined) {
      url += "?board_id=" + data.board_id;
    }
    url += "&board_type=2";
    this.sendGet(url, callback);
  }
  //获取未审核的帖子列表
  requestQuanziUncheckedList(data, callback) { 
    var url = Config.serverUrl + "/boards/posts/unchecked?page="
      +data.page+"&page_size=10&sort_id=2";
    if(data.board_id!==undefined) {
      url += "&board_id=" + data.board_id;
    }
    if(data.board_type!==undefined) {
      url += "&board_type=" + data.board_type;
    }
    this.sendGet(url, callback);
  }
  //获取未审核评论列表
  requestQuanziCommentsUncheckedList(data, callback) { 
    var url = Config.serverUrl + "/boards/posts/comments/unchecked?page="
      +data.page+"&page_size=10&sort_id=2";
    if(data.board_id!==undefined) {
      url += "&board_id=" + data.board_id;
    }
    if(data.board_type!==undefined) {
      url += "&board_type=" + data.board_type;
    }
    this.sendGet(url, callback);
  }
  //圈子模块-我关注的用户列表
  requestQuanziFollowTo(data, callback) { 
    var url = Config.serverUrl + "/boards/follow/to?page="
      +data.page+"&page_size=10";
    this.sendGet(url, callback);
  }
  //圈子模块-关注我的用户列表
  requestQuanziFollowMe(data, callback) { 
    var url = Config.serverUrl + "/boards/follow/me?page="
      +data.page+"&page_size=10";
    this.sendGet(url, callback);
  }
  //圈子模块-我关注的用户发的帖
  requestQuanziFollowToPosts(data, callback) { 
    var url = Config.serverUrl + "/boards/follow/to/posts?page="
      +data.page+"&page_size=10";
    this.sendGet(url, callback);
  }
  //关注某人
  requestFollowUser(data, callback) {
    this.sendPut(Config.serverUrl + '/boards/follow?user_id=' + data.user_id+"&status="+(data.status?1:0),
      callback);
  }
  //我的空间-我的帖子
  requestQuanziOwned(data, callback) { 
    var url = Config.serverUrl + "/boards/"+data.board_id+"/posts/owned?page="
      +data.page+"&page_size=10";
    this.sendGet(url, callback);
  }
  //我的空间-我的评论  board_type  版本块类型  2圈子，5心灵杂货铺
  requestQuanziMyposts(data, callback) { 
    var url = Config.serverUrl + "/boards/posts/myposts/comments?page="
      +data.page+"&page_size=500";
    if(data.board_type!==undefined) {
      url += "&board_type=" + data.board_type;
    }
    this.sendGet(url, callback);
  }
  //我的空间
  requestQuanziMyspace(data, callback) { 
    var url = Config.serverUrl + "/myself/myspace"
    this.sendGet(url, callback);
  }
  requestAdminListAction(data, callback) { 
    var url = Config.serverUrl + "/boards/board_admin_list?board_id="+data.board_id+"&page="
      +data.page+"&page_size="+data.page_size;
    this.sendGet(url, callback);
  }
  //我的空间-回复我的帖子评论列表
  requestQuanziReply2me(data, callback) { 
    var url = Config.serverUrl + "/boards/comment/reply2me?page="
      +data.page+"&page_size=10&board_type="+data.board_type;
    this.sendGet(url, callback);
  }
  //关注-关注圈子动态
  requestQuanziFollowPosts(data, callback) { 
    var url = Config.serverUrl + "/boards/follow/to/board_posts?page="
      +data.page+"&page_size=10";
    this.sendGet(url, callback);
  }

  //审核帖子或者评论  status: 0未审核 1审核通过 2审核不过
  requestQuanziPostOrCommet(data, callback) { 
    var url = Config.serverUrl + '/boards/check/post_or_commet?status=' + data.status;
    if(data.target_id!==undefined) {//帖子或者评论的id
      url += "&target_id=" + data.target_id;
    }
    if(data.target_type!==undefined) {//处理类型： 0帖子 1评论
      url += "&target_type=" + data.target_type;
    }
    this.sendPut(url,callback);
  }
  //找圈
  requestQuaziFind(data, callback) {
    var url = Config.serverUrl + "/boards/find";
    this.sendGet(url, callback);
  }
  requestQuaziMine(data, callback) {
    var url = Config.serverUrl + "/boards/mine";
    this.sendGet(url, callback);
  }
  //热门圈子  
  requestQuanziMoreRecommendList(data, callback) { 
    var url = Config.serverUrl + "/boards/recommend";
    this.sendGet(url, callback);
  }
   //创建群组 CreatePostPayload
  requestCreateQunliao(data, callback) {
    var sendData = {
      "desc": data.desc,
      "title": data.title,
       "image_urls": data.image_urls,
    }
    this.sendPost(Config.serverUrl + '/boards/'+data.board_id+'/chatgroups', JSON.stringify(sendData), callback);
  }

  // 我的圈子列表
  // http://dqtest.bestplus.me/dqapp/boards/favorited?page=1&page_size=10&sort_id=1&type=2
  sendQuanziList(data, callback) {
    var page_size = 10;
    if (data.page_size) {
      page_size=data.page_size;
    }
    var url = Config.serverUrl + "/boards/favorited?page=" + data.page + "&page_size="+page_size+"&sort_id=1&type=2";
    this.sendGet(url, callback);
  }

  // 圈子详情
  // http://dqtest.bestplus.me/dqapp/boards/{board_id}
  sendRequestQuanziContent(data, callback) {
    var url = Config.serverUrl + "/boards/" + data.board_id;
    this.sendGet(url, callback);
  }
  // 首页圈子推荐
  // http://43.241.76.189:40000/dqapp/boards/recommend
  sendRequestQuanziHoemTuijian(data, callback) {
    var url = Config.serverUrl + "/boards/recommend";
    this.sendGet(url, callback);
  }

  sendRequestQuanziPostList(data, callback) {
    var url = Config.serverUrl + "/boards/" + data.board_id+"/posts?page="+ data.page + "&page_size=" + data.page_size + "&sort_id=1&type=2";
    this.sendGet(url, callback);
  }
  sendQuanziIsAdminAction(data, callback) {
    var url = Config.serverUrl + "/boards/" + data.board_id+"/detail";
    this.sendGet(url, callback);
  }

  sendRequestQuanziPostContent(data, callback) {
    var url = Config.serverUrl + "/boards/" + data.board_id+"/posts/"+data.post_id;
    this.sendGet(url, callback);
  }
  requestReportPostOrCommet(data, callback) {
    var url = Config.serverUrl + "/boards/report/post_or_commet?reason="+ data.reason+ "&target_id=" + data.target_id;
    url += "&target_type=" + data.target_type;
    url += "&type=" + data.type;
    this.sendPut(url,callback);
  }
  requestUpdateTop(data, callback) {
    var url = Config.serverUrl + "/boards/posts/set_top?post_id=" + data.post_id+"&set_top=" + data.top;;
    this.sendPutNew(url,callback);
  }

  requestPostLike(data, callback) {
    this.sendPut(Config.serverUrl + '/boards/' + data.board_id + '/posts/' + data.post_id + "/like",
      callback);
  }
  requestPostShoucang(data, callback) {
    this.sendPut(Config.serverUrl + '/boards/' + data.board_id + '/posts/' + data.post_id + "/favorite",
      callback);
  }

  requestQuaziMembers(data,callback) {
    var url = Config.serverUrl + "/boards/" + data.board_id+"/favoriters?page="+ data.page + "&page_size=" + 10;
    this.sendGet(url, callback);
  }

  sendRequestQuanziCommentList(data, callback) {
    var url = Config.serverUrl + "/boards/" + data.board_id+"/posts/"+data.post_id+"/comments?page="+ data.page + "&page_size=" + 10 + "&sort_id=1&type=2";
    this.sendGet(url, callback);
  }

  sendQuanziPostComment(data, callback) {
    var sendData = {
      "content": data.comment,
    }
    if (data.comment_id!==undefined) {
      sendData.comment_id=data.comment_id;
      sendData.send_ids = data.send_ids;
    }
    this.sendPost(Config.serverUrl + '/boards/'+ data.board_id+'/posts/' + data.post_id + '/comments/create', JSON.stringify(sendData), callback);
  }
  //GET /dqapp/boards/{board_id}/posts/{post_id}/comments/comments/{comment_id}
  sendQuanziCommentShow(data, callback) {
    var url = Config.serverUrl + '/boards/'+ data.board_id+'/posts/' + data.post_id + '/comments/'+data.comment_id;
    url += "?page=1&page_size=200";
    this.sendGet(url, callback);
  }

  sendQuanziChatGroupList(data, callback) {
    var url = Config.serverUrl + "/boards/" + data.board_id+"/chatgroups?page="+ data.page + "&page_size=" + data.page_size + "&sort_id=1&type=2";
    this.sendGet(url, callback);
  }



  //http://dqtest.bestplus.me/dqapp/boards?page=1&page_size=10&sort_id=1&type=2
  //圈子随机列表
  requestQuanziRandomList(data, callback) {
    var url = Config.serverUrl + "/boards?page=" + data.page + "&page_size=" + data.page_size + "&sort_id=1&type=2";
    this.sendGet(url, callback);
  }

  //收藏圈子
  //http://dqtest.bestplus.me/dqapp/boards/1/favorite
  sendShoucangQuazi(data, callback) {
    var url = Config.serverUrl + "/boards/" + data.board_id + "/favorite";
    this.sendPutNew(url, callback);
  }

  requestQunData(data,callback){
    var url = Config.serverUrl + "/boards/" + data.board_id + "/chatgroups/"+data.chatgroup_id;
    this.sendGet(url, callback);
  }

  //http://dqtest.bestplus.me/dqapp/boards/1/favorite
  //退群
  requestQuitQun(data, callback) {
    var url = Config.serverUrl + "/boards/" + data.board_id + "/chatgroups/"+data.chatgroup_id+"/members/self";
    this.sendDelete(url,{}, callback);
  }

   //进群
  requestEnterQun(data, callback) {
     var sendData = {
      "content": data.content,

    }
    var url = Config.serverUrl + "/boards/" + data.board_id + "/chatgroups/"+data.chatgroup_id+"/members";
    this.sendPost(url, JSON.stringify(sendData), callback);
  }

  //http://dqtest.bestplus.me/dqapp/boards/1/chatgroups/1/members?page=1&page_size=5
  //请求群成员
  requestQunMemberList(data, callback) {
    var url = Config.serverUrl + "/boards/" + data.board_id + "/chatgroups/" + data.chatgroup_id +
      "/members?page=" + data.page + "&page_size=20";
    this.sendGet(url, callback);
  }

  requestQunliaoList(data, callback) {
    var url = "";
    //所有圈子的所有
    if(data.requestType === 0) {

    }
    //我的所有
    //http://dqtest.bestplus.me/dqapp/boards/chatgroups/owned?page=1&page_size=5
    else if(data.requestType === 1) {
      url = Config.serverUrl + "/boards/chatgroups/owned?page=" + data.page + "&page_size=10";
    }
    //圈子的所有
    //http://dqtest.bestplus.me/dqapp/boards/1/chatgroups?page=1&page_size=5&sort_id=1
    else if(data.requestType === 2) {
      url = Config.serverUrl + "/boards/" + data.board_id + "/chatgroups?page=" + data.page + "&page_size=10&sort_id=1";
    }
    this.sendGet(url, callback);
  }
  //GET /dqapp/boards/hotspots
  requestHostList(data, callback) {
    var url = Config.serverUrl + "/boards/hotspots?page=" + data.page + "&page_size=20";
    this.sendGet(url, callback);
  }

  sendRequestSearchContent(data, callback){  // data.board_id 圈子id
    var url = Config.serverUrl + "/boards/" + data.board_id + "/posts/searched?key=" + data.key + "&page=" + data.page + "&page_size=10";
    this.sendGet(url, callback);
  }

  //更新群信息
  //http://118.89.111.124/dqapp/boards/1/chatgroups/1
  //data{
//   "desc": "6",
//   "image_urls": [
//     "http://inews.gtimg.com/newsapp_ls/0/1570068275_200160/0",
//     "http://inews.gtimg.com/newsapp_ls/0/1570068275_200160/0",
//     "http://inews.gtimg.com/newsapp_ls/0/1570068275_200160/0"
//   ],
//   "title": "xzev"
// }
  sendUpdateQunData(data, callback) {
    var url = Config.serverUrl + "/boards/" + data.board_id + "/chatgroups/" + data.chatgroup_id;
    var sendData = {};
    if(data.desc) {
      sendData.desc = data.desc;
    }
    if(data.image_urls) {
      sendData.image_urls = data.image_urls;
    }
    if(data.title) {
      sendData.title = data.title;
    }
    this.sendPatchNew(url, sendData, callback);
  }

  // 管理员踢人
  //http://118.89.111.124/dqapp/boards/1/chatgroups/1/members/11/kick
  sendKickMember(data, callback) {
    var url = Config.serverUrl + "/boards/" + data.board_id + "/chatgroups/" + data.chatgroup_id + "/members/" + data.member_id + "/kick";
    this.sendDeleteNew(url, {}, callback);
  }

  //获取单个组未审核数量
  //http://118.89.111.124/dqapp/boards/11/chatgroups/1/members/members/unchecked/num
  sendGetMembersUncheckedNum(data, callback) {
    var url = Config.serverUrl + "/boards/" + data.board_id + "/chatgroups/" + data.chatgroup_id + "/members/unchecked/num";
    this.sendGet(url, callback);
  }

  //获取单个组未审核列表
  //http://118.89.111.124/dqapp/boards/1/chatgroups/1/members/unchecked?page=1&page_size=5
  sendGetMembersUncheckedList(data, callback) {
    var url = Config.serverUrl + "/boards/" + data.board_id + "/chatgroups/" + data.chatgroup_id + "/members/unchecked?page=" + data.page + "&page_size=10";
    this.sendGet(url, callback);
  }

  //http://118.89.111.124/dqapp/boards/1/chatgroups/1/members/11?accept=0
  //审核成员
  sendCheckMember(data, callback) {
    var url = Config.serverUrl + "/boards/" + data.board_id + "/chatgroups/" + data.chatgroup_id + "/members/" + data.member_id + "?accept=" + data.accept;
    this.sendPatchNew(url, {}, callback);
  }
  //PATCH /dqapp/boards/{board_id}
  sendUpdateQZSetting(data, callback) {
    var oj ={
      no_check:data.no_check,
      admin_id:data.admin_id,
      state:1,
    }
    var url = Config.serverUrl + "/boards/" + data.board_id;
    this.sendPatchNew(url, oj, callback);
  }

  //http://118.89.111.124/dqapp/boards
//  {
//   "desc": "pm",
//   "image_urls": [
//     "http://inews.gtimg.com/newsapp_ls/0/1570068275_200160/0",
//   ],
//   "title": "4"
// }
  //创建圈子
  sendCreateQuanzi(data, callback) {
    var url = Config.serverUrl + "/boards";
    this.sendPost(url, JSON.stringify(data), callback, ResponseConstants.responseType_text);
  }
  sendQuanziApply(data, callback) {
    var url = Config.serverUrl + "/boards/apply_for_dismiss?board_id="+data.board_id+"&reason="+data.reason;
    this.sendPutNew(url, callback);
  }
}
